﻿using Business;
using SqliteDataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace MVP
{
    public class LaporanBulananPresenter : BasePresenter<ILaporanBulananView>
    {
        private ILaporanBulananView _view;
        private LaporanBulananBusiness _business
        {
            get
            {
                return (LaporanBulananBusiness)Business;
            }
        }

        public LaporanBulananPresenter(ILaporanBulananView view)
            : base(view)
        {
            _view = view;
            Business = new LaporanBulananBusiness();
            _view.Save += OnViewSave;
        }

        private void OnViewSave(object sender, EventArgs e)
        {
            int rows = _business.Save();
            //_view.SendMessageInfo(String.Format("Update data berhasil ({0} baris).", rows));
        }

        protected override void OnViewInitialize(object sender, EventArgs e)
        {
            int rows = _business.InsertLaporanBulanan(_view.Bulan, Convert.ToInt32(_view.Tahun));
            //_view.SendMessageInfo("Rows loaded " + rows);
            rows = _business.LoadByBulanTahun(_view.Bulan, Convert.ToInt32(_view.Tahun));
            _view.LppbopDataSet = (DataSet)_business.LppbopSqlite;
        }

        public void ExportToExcel(string filename, DataSet dataset)
        {
            SqliteDataManager.LaporanBulananDataTable table = ((SqliteDataManager)dataset).LaporanBulanan;
            if (table.Count == 0)
            {
                _view.SendMessageInfo("No data to export.");
            }
            _business.ExportToExcel(filename, table);
        }

        public void Print(DataSet dataset)
        {
            SqliteDataManager.LaporanBulananDataTable table = ((SqliteDataManager)dataset).LaporanBulanan;
            if (table.Count == 0)
            {
                _view.SendMessageInfo("No data to print.");
            }
            _business.PrintExcel(table);
        }
    }
}
